package cn.shuto.liferay.plugin.chart.portlet.admin;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import cn.shuto.liferay.plugin.chart.service.model.Option;
import cn.shuto.liferay.plugin.chart.service.service.OptionLocalServiceUtil;
import cn.shuto.liferay.plugin.chart.service.util.JsonUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class ChartAdminPortlet
 */
public class ChartAdminPortlet extends MVCPortlet {
	/*
	 * 创建一个新的或修改一个已存在的Option
	 */
	public void addOrUpdateChartOption(ActionRequest request, ActionResponse response)
			throws PortalException, SystemException {
		ServiceContext serviceContext = ServiceContextFactory.getInstance(
				Option.class.getName(), request);
		
		long optionId = ParamUtil.getLong(request, "optionId");
		
		String name = ParamUtil.getString(request, "name");
		String description = ParamUtil.getString(request, "description");
		long chartTemplateId = ParamUtil.getLong(request, "chartTemplateId");
		long dataSourceCfgId = ParamUtil.getLong(request, "dataSourceCfgId");
		String dataSql = ParamUtil.getString(request, "dataSql");
		if(Validator.isNull(optionId)){
			OptionLocalServiceUtil.addOption(name, description, chartTemplateId,dataSourceCfgId,dataSql, serviceContext);
		}else{
			OptionLocalServiceUtil.updateOption(optionId, name, description, chartTemplateId, dataSourceCfgId,dataSql,serviceContext);
		}
	}
	/*
	 * 更新图表中对于的参数的值
	 */
	public void updateTemplateParamValue(ActionRequest request, ActionResponse response)throws PortalException, SystemException {
		ServiceContext serviceContext = ServiceContextFactory.getInstance(
				Option.class.getName(), request);
		long optionId = ParamUtil.getLong(request, "optionId");
		String paramvalue = JsonUtil.getTemplateParamValue(request);
		OptionLocalServiceUtil.updateTemplateParamValue(optionId, paramvalue, serviceContext);
	}
	/*
	 * 删除一个Option
	 */
	public void deleteChartOption(ActionRequest request, ActionResponse response)throws PortalException, SystemException {
		long optionId = ParamUtil.getLong(request, "optionId");
		OptionLocalServiceUtil.deleteOption(optionId);
	}
}
